@extends('layouts.admin')

@section('content')

<style type="text/css">
.delegate{margin:10px;}
</style>
<div class="page-content">

	<div class="row">

		<div class="col-xs-12">

			<h3 class="header smaller lighter blue">@if($dispatch == 1) 重新分配 @else 强制选择@endif</h3>

			<div class="table-header">

				<select id="supervisors"> 
					@foreach($supervisors as $supervisor)
					@if($supervisor->residue > -1000)
					<option value="{{$supervisor->id}}">
						{{$supervisor->user->name}}&nbsp;剩余名额：{{$supervisor->residue}}
					</option>
					@endif
					@endforeach
				</select>

			</div>

			<div class="table-responsive">

				<table id="sample-table-2" class="table table-striped table-bordered table-hover">
					<thead>
						<tr>
							<th class="center"><label>选择</label></th>
							<th>学生姓名</th>
							<th>班级</th>
							<th>手机</th>
							<th>邮箱</th>
							<th>当前选择的导师</th>
						</tr>
					</thead>
					<tbody>
						@foreach($students as $student)
						<tr>
							<td class="center">
								<label>
									<input onclick="check({{$student->id}})" type="checkbox" class="ace" id="{{$student->id}}"/>
									<span class="lbl"></span>
								</label>
							</td>
							<td>
								<a href="#">{{$student->user->name}}</a>
							</td>
							<td>{{$student->class}}</td>
							<td>
								{{$student->user->phone}}
							</td>
							<td>
								{{$student->user->email}}
							</td>
							<td>
								{{$student->supervisors()->first()->user->name}}
							</td>
						</tr>
						@endforeach
					</tbody>

				</table>

			</div>
			<div class="row">
				<div class="form-actions center delegate-list">
					<hr id="split-line">
					<button type="button" class="btn btn-sm btn-success center" onclick="submit();">
						提交
						<i class="icon-arrow-right  bigger-110" ></i>
					</button>
				</div>
			</div>
			<form style="display: none" id="twowayform" name="twowayform" class="form-horizontal" method="POST" action="/admin/enforcechoice">
                        {{ csrf_field() }}
                    	<input type="hidden" id="supervisor" name="supervisor" value="" />
                    	<input type="hidden" name="dispatch" value="{{$dispatch}}" />
            </form>
            @if ($edit != "")
			<div class="alert alert-danger">
				 	{{$edit}}
			</div>
			@endif
		</div>

	</div>

</div>

<script type="text/javascript">

	jQuery(function($) {

		var oTable1 = $('#sample-table-2').dataTable( {
			"aoColumns": [
		      { "bSortable": false },
		      null, null, null, null, null
			],
			"oLanguage": {
			"sLengthMenu": "每页显示 _MENU_ 条记录",
			"sZeroRecords": "抱歉， 没有找到",
			"sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
			"sInfoEmpty": "没有数据",
			"sInfoFiltered": "(从 _MAX_ 条数据中检索)",
			"oPaginate": {
				"sFirst": "首页",
				"sPrevious": "前一页",
				"sNext": "后一页",
				"sLast": "尾页"
			},
			"sZeroRecords": "没有检索到数据",
			}
		 });


		$('table th input:checkbox').on('click' , function(){

			var that = this;

			$(this).closest('table').find('tr > td:first-child input:checkbox')

			.each(function(){

				this.checked = that.checked;

				$(this).closest('tr').toggleClass('selected');

			});

				

		});

		$('[data-rel="tooltip"]').tooltip({placement: tooltip_placement});

		function tooltip_placement(context, source) {

			var $source = $(source);

			var $parent = $source.closest('table')

			var off1 = $parent.offset();

			var w1 = $parent.width();

	

			var off2 = $source.offset();

			var w2 = $source.width();

	

			if( parseInt(off2.left) < parseInt(off1.left) + parseInt(w1 / 2) ) return 'right';

			return 'left';

		}

		$('#gritter-center').on(ace.click_event, function(){

			$.gritter.add({

				title: 'This is a centered notification',

				text: 'Just add a "gritter-center" class_name to your $.gritter.add or globally to $.gritter.options.class_name',
				class_name: 'gritter-info gritter-center'
			});

	

			return false;

		});
	})

	function submit() {
		$('#supervisor').val($("#supervisors").find("option:selected").val());
		document.twowayform.submit()

	}
	function check(id) {
		var checkbox = $("table #" + id);
		if ($("table #" + id).prop("checked")) {
			$("#twowayform").append(
				"<input type='text' id ='input_" + id +  "' name='students[]' class='delegate' value='" + 
					id + "' />");
		} else {
			$("#input_" + id).remove();
		
		}
	}		
	function alert_num() {

		$( "#dialog-confirm" ).removeClass('hide').dialog({

				resizable: false,

				modal: true,

				title: "提示",

				title_html: true,

				buttons: [

					{

						html: "知道了",

						"class" : "btn btn-primary btn-xs",

						click: function() {

							$( this ).dialog( "close" );

						}

					}
				]

			});

	}
	
</script>


@endsection
